package be.vdab.servlets;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.vdab.DAO.PizzaDAO;

@WebServlet("/pizzas/tussenprijzen.htm")
public class PizzasTussenPrijzenServlets extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String VIEW = "/WEB-INF/JSP/pizzastussenprijzen.jsp";
	private final PizzaDAO pizzaDAO = new PizzaDAO();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (!request.getParameterMap().isEmpty()) {
			Map<String, String> fouten = new HashMap<String, String>();
			BigDecimal van = null;
			BigDecimal tot = null;
			try {
				van = new BigDecimal(request.getParameter("van"));
			} catch (Exception ex) {
				fouten.put("van", "tik een getal");
			}
			try {
				tot = new BigDecimal(request.getParameter("tot"));
			} catch (Exception ex) {
				fouten.put("tot", "tik een getal");
			}
			if (fouten.isEmpty()) {
				request.setAttribute("pizzas", pizzaDAO.findByPrijsBetween(van, tot));
			}
			request.setAttribute("fouten", fouten);
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher(VIEW);
		dispatcher.forward(request, response);
	}
}
